package cn.yudao.system.controller;

import cn.yudao.basic.annotation.PreAuthorize;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.system.domain.SystemDetail;
import cn.yudao.system.query.SystemDetailQuery;
import cn.yudao.system.service.ISystemDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/type")
public class SystemDetailController {

    @Autowired
    private ISystemDetailService service;



    @GetMapping
    @PreAuthorize(name = "查询所有数据字典",sn = "SystemDetail:list")
    public List<SystemDetail> loadAll(){
        List<SystemDetail> sytemDetails = service.loadAll();
        return sytemDetails;
    }

    @PostMapping
    @PreAuthorize(name = "查询数据字典",sn = "SystemDetail:query")
    public PageList<SystemDetail> queryType(@RequestBody SystemDetailQuery sytemDetailQuery){
        PageList<SystemDetail> query = service.query(sytemDetailQuery);
        return query;
    }

    @PutMapping
    @PreAuthorize(name = "新增/修改数据字典",sn = "SystemDetail:addOrupt")
    public AjaxResult addOrupt(@RequestBody SystemDetail sytemDetail){
        try {
            if (sytemDetail.getId() == null){
                service.save(sytemDetail);
            }else {
                service.upadte(sytemDetail);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除数据字典",sn = "SystemDetail:delete")
    public AjaxResult remove(@PathVariable Long id){
        try {
            service.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    @PatchMapping
    public AjaxResult patchRom(@RequestBody Long[] ids){
        try {
            service.patchRom(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
